package cn.eric.framework.ajax;

import cn.eric.framework.utils.general.merge.EMerge;
import com.google.gson.annotations.SerializedName;

/**
 * AJAX返回的通用结构
 */
public class AjaxResponse<T> {
    private static final int SC_SUCCESS = 0;
    private static final String INFO_SUCCESS = "success";
    private static final int SC_SERVER_ERROR = 500;
    private static final String INFO_SERVER_ERROR = "unknown error";

    public static AjaxResponse EMPTY_SUCCESS_RESPONSE;

    static {
        EMPTY_SUCCESS_RESPONSE = new AjaxResponse();
        EMPTY_SUCCESS_RESPONSE.setSc(SC_SUCCESS);
        EMPTY_SUCCESS_RESPONSE.setInfo(INFO_SUCCESS);
    }

    // 状态码
    @SerializedName("ret")
    private int sc;
    // 请求的数据
    private T data;
    // response的附带信息 比如错误信息
    @SerializedName("msg")
    private String info;

    // 分页
    @SerializedName("page_total")
    private int totalPage;
    @SerializedName("page_num")
    private int currentPage;
    @SerializedName("page_size")
    private int rowPerPage;
    @SerializedName("total_row")
    private long totalRow;

    /**
     * 用于unexpected error
     */
    public AjaxResponse() {
        this.sc = SC_SERVER_ERROR;
        this.info = INFO_SERVER_ERROR;
    }

    /**
     * 用于成功的 无分页的请求
     *
     * @param data 请求的数据
     */
    public AjaxResponse(T data) {
        this.data = data;
        this.sc = SC_SUCCESS;
        this.info = INFO_SUCCESS;
    }

    /**
     * 用于成功的 拥有分页的请求
     *
     * @param data    返回的具体数据
     * @param context 分页的信息
     */
    public AjaxResponse(T data, EricContext context) {
        this.data = data;

        EMerge.overwrite(this, context);

        this.sc = SC_SUCCESS;
        this.info = INFO_SUCCESS;
    }

    /**
     * 用于业务逻辑错误
     *
     * @param sc   状态码
     * @param info 错误提示
     */
    public AjaxResponse(int sc, String info) {
        this.sc = sc;
        this.info = info;
    }

    @Override
    public String toString() {
        return "AjaxResponse{" +
                "sc=" + sc +
                ", data=" + data +
                ", info='" + info + '\'' +
                '}';
    }

    public int getSc() {
        return sc;
    }

    public void setSc(int sc) {
        this.sc = sc;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getRowPerPage() {
        return rowPerPage;
    }

    public void setRowPerPage(int rowPerPage) {
        this.rowPerPage = rowPerPage;
    }

    public long getTotalRow() {
        return totalRow;
    }

    public void setTotalRow(long totalRow) {
        this.totalRow = totalRow;
    }
}

